﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
using System.Windows.Threading;
using System.ServiceModel;
using System.Configuration;
using ContractLibrary;
using ContractLibrary.Data;


namespace ServerSide
{
    /// <summary>
    /// Logique d'interaction pour MainWindow.xaml
    /// </summary>
    public partial class ServerMenu : Window
    {
        public Server server; // = new Server();
        public static ServiceHost duplexServiceHost;
        public static GameEngine engine;

        public ServerMenu()
        {
            InitializeComponent();
            Program.serverMenu = this;
            duplexServiceHost = new ServiceHost(typeof(Server));
            string serverIp = ConfigurationManager.AppSettings["ServerIP"];
            duplexServiceHost.AddServiceEndpoint(typeof(IContractServer), new NetTcpBinding(SecurityMode.None), "net.tcp://" + serverIp + ":9080/DataService");
            duplexServiceHost.Open();
        }
        public void refreshDataGrids(List<Player> players)
        {
            this.dataGrid_TeamBlue.ItemsSource = players.Where(x => (x != null && x.isInTeam(Constants.BLUE_TEAM_ID)));
            this.dataGrid_TeamRed.ItemsSource = players.Where(x => (x != null && x.isInTeam(Constants.RED_TEAM_ID)));
        }

        private void button_start_Click(object sender, RoutedEventArgs e)
        {
            this.startGame();
        }

        public void startGame()
        {

            //Génération du plateau
            engine = GameEngine.getInstanceInit((int)sliderNbCases.Value, (int)sliderWallDensity.Value);


            //Informe les clients du commencement du jeux
            foreach (KeyValuePair<ICallBackClient, Player> pair_C_P in Server.playersCallBackMap)
            {
                if (pair_C_P.Value != null && pair_C_P.Value.state == PlayerStates.inWait)
                {
                    pair_C_P.Value.state = PlayerStates.inGame;
                    pair_C_P.Key.startGame();
                    pair_C_P.Key.getElements(engine.Map.elements);
                }
            }
            
        }

        private void sliderNbCases_ValueChanged(object sender, RoutedPropertyChangedEventArgs<double> e)
        {
            this.txtNbCases.Content = "Nombre de cases : " + sliderNbCases.Value;
        }

        private void sliderWallDensity_ValueChanged(object sender, RoutedPropertyChangedEventArgs<double> e)
        {
            this.txtNbMurs.Content = "Densité de murs en % : " + sliderWallDensity.Value;
        }
        

       
    }

}
